package com.moleq.mgpos.activities;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.AbsoluteLayout;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.moleq.adapter.SelectionAdapter;
import com.moleq.dialog.MessageDialog;
import com.moleq.dialog.MessageDialog.OnClickMessageListener;
import com.moleq.itemsetting.tools.MGCreateFiles;
import com.moleq.mgpos.widget.BeepImageButton;
import com.moleq.models.AdapterDataSourceBuilder;
import com.moleq.models.Pinpad;
import com.moleq.models.Scale;
import com.moleq.models.Scanner;
import com.moleq.models.SettingSelection;
import com.moleq.service.PinPadService;
import com.moleq.service.ScaleService;
import com.moleq.service.ScannerService;
import com.moleq.service.SelectionService;
import com.moleq.service.ServiceManager;
import com.moleq.util.MoleQUtil;

public class HardwareSettingActivity extends Activity {
	/** Called when the activity is first created. */

	private LinearLayout ll_scale, ll_scanner, ll_pinpad;
	private AbsoluteLayout al_scale, al_scanner, al_pinpad;
	private SelectionAdapter chooseAdapter = null;
	private HardwareListAdapter scaleListAdapter = null;
	private SelectionAdapter selectionAdapter = null;
	private HardwareListAdapter scannerListAdapter = null;
	private HardwareListAdapter pinpadAdapter = null;
	private ListView lv_choose, lv_scale_list, lv_scanner_list, lv_pinpad_list;
	private List<Scale> ScaleSource = null;
	private List<SettingSelection> SelectionSource = null;
	private List<Scanner> ScannerSource = null;
	private List<Pinpad> PinpadSource = null;
	private MessageDialog mMessageDialog;
	private Boolean isDelete = false;
	private Boolean isDialogItem = false;
	private Button btn_scale_port, btn_scale_baud, btn_scale_parity,
			btn_scale_data_bits, btn_scale_stop_bits, btn_scale_weight_cmd,
			btn_scale_cancel_cmd, btn_scale_divide_by;
	private EditText et_scale_time_out, et_scale_data, et_scale_device_name;
	private CheckBox cb_scale_active;

	private Button btn_scanner_port, btn_scanner_baud, btn_scanner_parity,
			btn_scanner_data_bits, btn_scanner_stop_bits,
			btn_scanner_disable_cmd, btn_scanner_enable_cmd;
//			btn_scanner_weight_format;
	private EditText et_scanner_data, et_scanner_device_name;
	private CheckBox cb_scanner_active;

	private Button btn_pinpad_port,btn_conn_type;
	private EditText et_pinpad_device_name, et_pinpad_timeout,
			et_pinpad_destip,et_pinpad_destport;
	private CheckBox cb_pinpad_active,cb_signature;

	private Button btn_save, btn_exit, btn_add, btn_delete, btn_undo;

	private ListView lv_dialog_list;
	private Button btn_dialog_ok, btn_dialog_cancel;
	private BeepImageButton iv_dialog_add, iv_dialog_delete, iv_dialog_complete;
	private EditText et_dialog_add;

	private ScaleService scaleService;
	private ScannerService scannerService;
	private SelectionService selectionService;
	private PinPadService pinpadService;
	private int ScaleControlID;

	public static final int SCALE_SETTING = 1;
	public static final int SCANNER_SETTING = 0;
	public static final int PINPAD_SETTING = 2;
	public static int CurrentMode = 0;

	public static final int SYSTEM_EDITED = 0;
	public static final int USER_EDITED = 1;
	public static int editChangeMode = 0;
	//
	public static final int Update_action = 0;
	public static final int Insert_action = 1;
	public static int currentProcessMode = 0;
	private MGCreateFiles mMGCreateFiles = new MGCreateFiles();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_hardware_setting);

		ControlFindId();
		scaleService = ServiceManager.getInstance().mScaleService;
		scannerService = ServiceManager.getInstance().mScannerService;
		selectionService = ServiceManager.getInstance().mSelectionService;
		pinpadService = ServiceManager.getInstance().mPinPadService;

		ArrayList<String> selectionItems = new ArrayList<String>();
		selectionItems.add("Scanner");
		selectionItems.add("Scale");
		selectionItems.add("Pinpad");
		
		chooseAdapter = new SelectionAdapter(this, selectionItems);
		chooseAdapter.setItemTextSize(20);
		chooseAdapter.ScrollToRow(0);
		
		ScaleSource = scaleService.getAllScales();
		if (scaleListAdapter == null)
			scaleListAdapter = new HardwareListAdapter(this, ScaleSource);

		ScannerSource = scannerService.getAllScanner();
		if (scannerListAdapter == null)
			scannerListAdapter = new HardwareListAdapter(this, ScannerSource);

		PinpadSource = pinpadService.getAllPinpad();
		if (pinpadAdapter == null)
			pinpadAdapter = new HardwareListAdapter(this, PinpadSource);

		lv_scale_list.setAdapter(scaleListAdapter);
		lv_scanner_list.setAdapter(scannerListAdapter);
		lv_pinpad_list.setAdapter(pinpadAdapter);
		lv_choose.setAdapter(chooseAdapter);
		lv_scale_list.setOnItemClickListener(onItemClickListener);
		lv_scanner_list.setOnItemClickListener(onItemClickListener);
		lv_pinpad_list.setOnItemClickListener(onItemClickListener);
		lv_choose.setOnItemClickListener(onItemClickListener);

		btn_scale_port.setOnClickListener(onClickListener);
		btn_scale_baud.setOnClickListener(onClickListener);
		btn_scale_parity.setOnClickListener(onClickListener);
		btn_scale_data_bits.setOnClickListener(onClickListener);
		btn_scale_stop_bits.setOnClickListener(onClickListener);
		btn_scale_weight_cmd.setOnClickListener(onClickListener);
		btn_scale_cancel_cmd.setOnClickListener(onClickListener);
		btn_scale_divide_by.setOnClickListener(onClickListener);

		cb_scale_active.setOnCheckedChangeListener(onCheckedChangeListener);

		btn_scale_port.addTextChangedListener(watcher);
		btn_scale_baud.addTextChangedListener(watcher);
		btn_scale_parity.addTextChangedListener(watcher);
		btn_scale_data_bits.addTextChangedListener(watcher);
		btn_scale_stop_bits.addTextChangedListener(watcher);
		btn_scale_weight_cmd.addTextChangedListener(watcher);
		btn_scale_cancel_cmd.addTextChangedListener(watcher);
		btn_scale_divide_by.addTextChangedListener(watcher);
		et_scale_time_out.addTextChangedListener(watcher);
		et_scale_data.addTextChangedListener(watcher);
		et_scale_device_name.addTextChangedListener(watcher);

		btn_scanner_port.setOnClickListener(onClickListener);
		btn_scanner_baud.setOnClickListener(onClickListener);
		btn_scanner_parity.setOnClickListener(onClickListener);
		btn_scanner_data_bits.setOnClickListener(onClickListener);
		btn_scanner_stop_bits.setOnClickListener(onClickListener);
		btn_scanner_disable_cmd.setOnClickListener(onClickListener);
		btn_scanner_enable_cmd.setOnClickListener(onClickListener);
//		btn_scanner_weight_format.setOnClickListener(onClickListener);

		cb_scanner_active.setOnCheckedChangeListener(onCheckedChangeListener);

		btn_scanner_port.addTextChangedListener(watcher);
		btn_scanner_baud.addTextChangedListener(watcher);
		btn_scanner_parity.addTextChangedListener(watcher);
		btn_scanner_data_bits.addTextChangedListener(watcher);
		btn_scanner_stop_bits.addTextChangedListener(watcher);
		btn_scanner_disable_cmd.addTextChangedListener(watcher);
		btn_scanner_enable_cmd.addTextChangedListener(watcher);
//		btn_scanner_weight_format.addTextChangedListener(watcher);
		et_scanner_data.addTextChangedListener(watcher);
		et_scanner_device_name.addTextChangedListener(watcher);

		btn_pinpad_port.setOnClickListener(onClickListener);
		btn_conn_type.setOnClickListener(onClickListener);

		cb_pinpad_active.setOnCheckedChangeListener(onCheckedChangeListener);
		cb_signature.setOnCheckedChangeListener(onCheckedChangeListener);

		btn_pinpad_port.addTextChangedListener(watcher);
		et_pinpad_device_name.addTextChangedListener(watcher);
		et_pinpad_timeout.addTextChangedListener(watcher);
		et_pinpad_destip.addTextChangedListener(watcher);
		btn_conn_type.addTextChangedListener(watcher);
		et_pinpad_destport.addTextChangedListener(watcher);

		al_scale.setOnClickListener(onClickListener);
		al_scanner.setOnClickListener(onClickListener);
		al_pinpad.setOnClickListener(onClickListener);

		btn_save.setOnClickListener(onClickListener);
		btn_exit.setOnClickListener(onClickListener);
		btn_add.setOnClickListener(onClickListener);
		btn_delete.setOnClickListener(onClickListener);
		btn_undo.setOnClickListener(onClickListener);

		editChangeMode = SYSTEM_EDITED;
		CurrentMode = SCANNER_SETTING;
		GetScannerDetail(0);
		editChangeMode = USER_EDITED;

		ll_scanner.setVisibility(View.VISIBLE);
		ll_scale.setVisibility(View.GONE);
		ll_pinpad.setVisibility(View.GONE);
	}

	public OnItemClickListener onItemClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			// TODO Auto-generated method stub
			switch (parent.getId()) {
			case R.id.lv_choose:
				chooseAdapter.ScrollToRow(position);
				switch (position) {
				case SCALE_SETTING:
					ll_scale.setVisibility(View.VISIBLE);
					ll_scanner.setVisibility(View.GONE);
					ll_pinpad.setVisibility(View.GONE);
					CurrentMode = SCALE_SETTING;
					editChangeMode = SYSTEM_EDITED;
					GetScaleDetail(0);
					scaleListAdapter.ScrollToRow(0);
					lv_scale_list.setSelection(0);
					editChangeMode = USER_EDITED;
					break;
				case SCANNER_SETTING:
					ll_scale.setVisibility(View.GONE);
					ll_scanner.setVisibility(View.VISIBLE);
					ll_pinpad.setVisibility(View.GONE);
					CurrentMode = SCANNER_SETTING;
					editChangeMode = SYSTEM_EDITED;
					GetScannerDetail(0);
					scannerListAdapter.ScrollToRow(0);
					lv_scanner_list.setSelection(0);
					editChangeMode = USER_EDITED;
					break;
				case PINPAD_SETTING:
					ll_scale.setVisibility(View.GONE);
					ll_scanner.setVisibility(View.GONE);
					ll_pinpad.setVisibility(View.VISIBLE);
					CurrentMode = PINPAD_SETTING;
					editChangeMode = SYSTEM_EDITED;
					GetPinpadDetail(0);
					pinpadAdapter.ScrollToRow(0);
					lv_pinpad_list.setSelection(0);
					editChangeMode = USER_EDITED;
				}
				break;
			case R.id.lv_scale_listview:
				scaleListAdapter.ScrollToRow(position);
				editChangeMode = SYSTEM_EDITED;
				GetScaleDetail(position);
				editChangeMode = USER_EDITED;
				break;
			case R.id.lv_scanner_listview:
				scannerListAdapter.ScrollToRow(position);
				editChangeMode = SYSTEM_EDITED;
				GetScannerDetail(position);
				editChangeMode = USER_EDITED;
				break;
			case R.id.lv_pinpad_listview:
				pinpadAdapter.ScrollToRow(position);
				editChangeMode = SYSTEM_EDITED;
				GetPinpadDetail(position);
				editChangeMode = USER_EDITED;
				break;
			}
		}
	};

	public OnClickListener onClickListener = new OnClickListener() {
		int position = 0;

		@Override
		public void onClick(View view) {
			// TODO Auto-generated method stub
			switch (view.getId()) {
			case R.id.btn_add:
				switch (CurrentMode) {
				case SCALE_SETTING:
					setEmpty();
					SetEnabledFalse();
					et_scale_device_name.requestFocus();
					break;
				case SCANNER_SETTING:
					setEmpty();
					SetEnabledFalse();
					et_scanner_device_name.requestFocus();
					break;
				case PINPAD_SETTING:
					setEmpty();
					SetEnabledFalse();
					et_pinpad_device_name.requestFocus();
					break;
				}
				currentProcessMode = Insert_action;
				break;
			case R.id.btn_delete:
				switch (CurrentMode) {
				case SCALE_SETTING:
					if (ScaleSource.size() <= 0)
						return;
					break;
				case SCANNER_SETTING:
					if (ScannerSource.size() <= 0)
						return;
					break;
				case PINPAD_SETTING:
					if (PinpadSource.size() <= 0)
						return;
					break;
				}
				isDelete = true;
				ShowMessage("Are you sure to delete this item?",
						mMessageDialog.MESSAGE_BOX_YES_NO,
						mMessageDialog.ICON_INFO);
				break;
			case R.id.btn_undo:
				switch (CurrentMode) {
				case SCALE_SETTING:
					position = scaleListAdapter.GetSelectedIndex();
					GetScaleDetail(position);
					break;
				case SCANNER_SETTING:
					position = scannerListAdapter.GetSelectedIndex();
					GetScannerDetail(position);
					break;
				case PINPAD_SETTING:
					position = pinpadAdapter.GetSelectedIndex();
					GetPinpadDetail(position);
					break;
				}
				SetEnabledTrue();
				currentProcessMode = Update_action;
				break;
			case R.id.btn_save:
				String errorString = "";
				switch (CurrentMode) {
				case SCALE_SETTING:
					if (currentProcessMode == Update_action) {
						if (ScaleSource.size() <= 0)
							return;
						Scale scale = GetScaleItem();
						if (scale == null)
							return;
						errorString = scaleService.updateScalesItem(scale);
//						if (GlobalInfo.getInstance().isMGNetwork) {
//							if (scale != null) {
//								mMGCreateFiles.createrFile(scale
//										.toMGData("UPDATE"));// UPDATE,DELETE
//							}
//						}
					} else if (currentProcessMode == Insert_action) {
						Scale scale = GetScaleItem();
						if (scale == null)
							return;
						errorString = scaleService.insertItem(scale);
//						if (GlobalInfo.getInstance().isMGNetwork) {
//							if (scale != null) {
//								mMGCreateFiles.createrFile(scale
//										.toMGData("INSERT"));// UPDATE,DELETE
//							}
//						}
					}
					ScaleSource.clear();
					ScaleSource = scaleService.getAllScales();
					scaleListAdapter.setSource(ScaleSource);
					scaleListAdapter.notifyDataSetChanged();
					if (currentProcessMode == Insert_action) {
						scaleListAdapter.ScrollToRow(ScaleSource.size() - 1);
						lv_scale_list.setSelection(ScaleSource.size() - 1);
					}
					break;
				case SCANNER_SETTING:
					if (currentProcessMode == Update_action) {
						if (ScannerSource.size() <= 0)
							return;
						Scanner scanner = GetScannerItem();
						if (scanner == null)
							return;
						errorString = scannerService.updateScannerItem(scanner);
//						if (GlobalInfo.getInstance().isMGNetwork) {
//							if (scanner != null) {
//								mMGCreateFiles.createrFile(scanner
//										.toMGData("UPDATE"));// UPDATE,DELETE
//							}
//						}
					} else if (currentProcessMode == Insert_action) {
						Scanner scanner = GetScannerItem();
						if (scanner == null)
							return;
						errorString = scannerService.insertItem(scanner);
//						if (GlobalInfo.getInstance().isMGNetwork) {
//							if (scanner != null) {
//								mMGCreateFiles.createrFile(scanner
//										.toMGData("INSERT"));// UPDATE,DELETE
//							}
//						}
					}
					ScannerSource.clear();
					ScannerSource = scannerService.getAllScanner();
					scannerListAdapter.setSource(ScannerSource);
					scannerListAdapter.notifyDataSetChanged();
					if (currentProcessMode == Insert_action) {
						scannerListAdapter
								.ScrollToRow(ScannerSource.size() - 1);
						lv_scanner_list.setSelection(ScannerSource.size() - 1);
					}
					break;
				case PINPAD_SETTING:
					if (currentProcessMode == Update_action) {
						if (PinpadSource.size() <= 0)
							return;
						Pinpad pinpad = GetPinpadItem();
						if (pinpad == null)
							return;
						errorString = pinpadService.updatePinpad(pinpad);
//						if (GlobalInfo.getInstance().isMGNetwork) {
//							if (pinpad != null) {
//								mMGCreateFiles.createrFile(pinpad
//										.toMGData("UPDATE"));// UPDATE,DELETE
//							}
//						}
					} else if (currentProcessMode == Insert_action) {
						Pinpad pinpad = GetPinpadItem();
						if (pinpad == null)
							return;
						errorString = pinpadService.insertPinpad(pinpad);
//						if (GlobalInfo.getInstance().isMGNetwork) {
//							if (pinpad != null) {
//								mMGCreateFiles.createrFile(pinpad
//										.toMGData("INSERT"));// UPDATE,DELETE
//							}
//						}
					}
					PinpadSource.clear();
					PinpadSource = pinpadService.getAllPinpad();
					pinpadAdapter.setSource(PinpadSource);
					pinpadAdapter.notifyDataSetChanged();
					if (currentProcessMode == Insert_action) {
						pinpadAdapter.ScrollToRow(PinpadSource.size() - 1);
						lv_pinpad_list.setSelection(PinpadSource.size() - 1);
					}
					break;
				}
				if (errorString.length() > 0) {
					ShowMessage("Save failed.", mMessageDialog.MESSAGE_BOX_OK,
							mMessageDialog.ICON_INFO);
				} else {
					Toast.makeText(HardwareSettingActivity.this,
							"Save completed.", Toast.LENGTH_SHORT).show();
				}
				SetEnabledTrue();
				currentProcessMode = Update_action;
				break;
			case R.id.btn_exit:
				ShowMessage(
						getResources().getString(R.string.msg_exit_program),
						mMessageDialog.MESSAGE_BOX_YES_NO,
						mMessageDialog.ICON_INFO);
				break;
			case R.id.al_scale:
				Keyboard();
				break;
			case R.id.al_scanner:
				Keyboard();
				break;
			case R.id.al_pinpad:
				Keyboard();
				break;
			case R.id.btn_scale_port:

				DialogWindows("PORT", String.valueOf(btn_scale_port.getText()));
				ScaleControlID = R.id.btn_scale_port;
				break;
			case R.id.btn_scale_baud:
				DialogWindows("BAUD", String.valueOf(btn_scale_baud.getText()));
				ScaleControlID = R.id.btn_scale_baud;
				break;
			case R.id.btn_scale_parity:
				DialogWindows("PARITY",
						String.valueOf(btn_scale_parity.getText()));
				ScaleControlID = R.id.btn_scale_parity;
				break;
			case R.id.btn_scale_data_bits:
				DialogWindows("DATABITS",
						String.valueOf(btn_scale_data_bits.getText()));
				ScaleControlID = R.id.btn_scale_data_bits;
				break;
			case R.id.btn_scale_stop_bits:
				DialogWindows("STOPBITS",
						String.valueOf(btn_scale_stop_bits.getText()));
				ScaleControlID = R.id.btn_scale_stop_bits;
				break;
			case R.id.btn_scale_weight_cmd:
				DialogWindows("WEIGHTCMD",
						String.valueOf(btn_scale_weight_cmd.getText()));
				ScaleControlID = R.id.btn_scale_weight_cmd;
				break;
			case R.id.btn_scale_cancel_cmd:
				DialogWindows("SCALE CANCEL",
						String.valueOf(btn_scale_cancel_cmd.getText()));
				ScaleControlID = R.id.btn_scale_cancel_cmd;
				break;
			case R.id.btn_scale_divide_by:
				DialogWindows("DIVIDE BY",
						String.valueOf(btn_scale_divide_by.getText()));
				ScaleControlID = R.id.btn_scale_divide_by;
				break;
			case R.id.btn_scanner_port:
				DialogWindows("PORT",
						String.valueOf(btn_scanner_port.getText()));
				ScaleControlID = R.id.btn_scanner_port;
				break;
			case R.id.btn_scanner_baud:
				DialogWindows("BAUD",
						String.valueOf(btn_scanner_baud.getText()));
				ScaleControlID = R.id.btn_scanner_baud;
				break;
			case R.id.btn_scanner_parity:
				DialogWindows("PARITY",
						String.valueOf(btn_scanner_parity.getText()));
				ScaleControlID = R.id.btn_scanner_parity;
				break;
			case R.id.btn_scanner_data_bits:
				DialogWindows("DATABITS",
						String.valueOf(btn_scanner_data_bits.getText()));
				ScaleControlID = R.id.btn_scanner_data_bits;
				break;
			case R.id.btn_scanner_stop_bits:
				DialogWindows("STOPBITS",
						String.valueOf(btn_scanner_stop_bits.getText()));
				ScaleControlID = R.id.btn_scanner_stop_bits;
				break;
			case R.id.btn_scanner_disable_cmd:
				DialogWindows("SCANNER DISABLE",
						String.valueOf(btn_scanner_disable_cmd.getText()));
				ScaleControlID = R.id.btn_scanner_disable_cmd;
				break;
			case R.id.btn_scanner_enable_cmd:
				DialogWindows("SCANNER ENABLE",
						String.valueOf(btn_scanner_enable_cmd.getText()));
				ScaleControlID = R.id.btn_scanner_enable_cmd;
				break;
//			case R.id.btn_scanner_weight_format:
//				DialogWindows("WEIGHT",
//						String.valueOf(btn_scanner_weight_format.getText()));
//				ScaleControlID = R.id.btn_scanner_weight_format;
//				break;
			case R.id.btn_pinpad_port:
				DialogWindows("PORT", String.valueOf(btn_pinpad_port.getText()));
				ScaleControlID = R.id.btn_pinpad_port;
				break;
			case R.id.btn_conn_type:
				DialogWindows("CONNTYPE", String.valueOf(btn_conn_type.getText()));
				ScaleControlID = R.id.btn_conn_type;
				break;
			}
		}
	};

	OnClickListener OnClickDialogWindowListener = new OnClickListener() {

		@Override
		public void onClick(View view) {
			// TODO Auto-generated method stub
			switch (view.getId()) {
			case R.id.btn_dialog_ok:
				if (SelectionSource.size() > 0) {
					String value = SelectionSource.get(
							selectionAdapter.GetSelectedIndex()).getValue();
					Button btn_ScaleControl = (Button) findViewById(ScaleControlID);
					btn_ScaleControl.setText(value);
				}
				DialogWindow.dismiss();
				break;
			case R.id.btn_dialog_cancel:
				DialogWindow.dismiss();
				break;
			case R.id.iv_dialog_add:
				lv_dialog_list.setEnabled(false);
				btn_dialog_ok.setEnabled(false);
				btn_dialog_cancel.setEnabled(false);
				iv_dialog_delete.setEnabled(false);
				et_dialog_add.setVisibility(View.VISIBLE);
				iv_dialog_complete.setVisibility(View.VISIBLE);
				iv_dialog_add.setVisibility(View.GONE);
				break;
			case R.id.iv_dialog_complete:
				lv_dialog_list.setEnabled(true);
				btn_dialog_ok.setEnabled(true);
				btn_dialog_cancel.setEnabled(true);
				iv_dialog_delete.setEnabled(true);
				et_dialog_add.setVisibility(View.INVISIBLE);
				String Value = String.valueOf(et_dialog_add.getText());
				if (Value != null && !Value.trim().equals("")) {
					String Group = String.valueOf(lv_dialog_list.getTag());
					int Max = selectionService.GetSelectionOrder(Group);
					String errorString = selectionService.InsertSelection(
							Group, Value, Max);
					if (errorString.length() > 0) {
						ShowMessage("Save Failure.",
								mMessageDialog.MESSAGE_BOX_OK,
								mMessageDialog.ICON_INFO);
					} else {
						SelectionSource = selectionService.getSelections(Group);
						selectionAdapter
								.setSource(AdapterDataSourceBuilder.readValueFromSettings(SelectionSource));
						selectionAdapter.notifyDataSetChanged();
					}
				}
				et_dialog_add.setText("");
				iv_dialog_complete.setVisibility(View.GONE);
				iv_dialog_add.setVisibility(View.VISIBLE);
				break;
			case R.id.iv_dialog_delete:
				if (SelectionSource.size() <= 0)
					return;
				isDelete = true;
				isDialogItem = true;
				ShowMessage("Are you sure to delete this item?",
						mMessageDialog.MESSAGE_BOX_YES_NO,
						mMessageDialog.ICON_INFO);
				break;
			}
		}

	};

	OnItemClickListener onItemClickDialogWindowListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			// TODO Auto-generated method stub
			selectionAdapter.ScrollToRow(position);
		}

	};

	private TextWatcher watcher = new TextWatcher() {

		@Override
		public void afterTextChanged(Editable arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
				int arg3) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onTextChanged(CharSequence arg0, int arg1, int arg2,
				int arg3) {
			// TODO Auto-generated method stub
			if (editChangeMode == USER_EDITED) {
				SetEnabledFalse();
			}
		}

	};

	OnCheckedChangeListener onCheckedChangeListener = new OnCheckedChangeListener() {

		@Override
		public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
			// TODO Auto-generated method stub
			if (editChangeMode == USER_EDITED) {
				SetEnabledFalse();
			}
		}

	};

	public void ControlFindId() {
		btn_scale_port = (Button) findViewById(R.id.btn_scale_port);
		btn_scale_baud = (Button) findViewById(R.id.btn_scale_baud);
		btn_scale_parity = (Button) findViewById(R.id.btn_scale_parity);
		btn_scale_data_bits = (Button) findViewById(R.id.btn_scale_data_bits);
		btn_scale_stop_bits = (Button) findViewById(R.id.btn_scale_stop_bits);
		btn_scale_weight_cmd = (Button) findViewById(R.id.btn_scale_weight_cmd);
		btn_scale_cancel_cmd = (Button) findViewById(R.id.btn_scale_cancel_cmd);
		btn_scale_divide_by = (Button) findViewById(R.id.btn_scale_divide_by);
		et_scale_time_out = (EditText) findViewById(R.id.et_scale_time_out);
		et_scale_data = (EditText) findViewById(R.id.et_scale_data);
		et_scale_device_name = (EditText) findViewById(R.id.et_scale_device_name);
		ll_scale = (LinearLayout) findViewById(R.id.ll_scale);
		cb_scale_active = (CheckBox) findViewById(R.id.cb_scale_active);
		lv_scale_list = (ListView) findViewById(R.id.lv_scale_listview);

		ll_scanner = (LinearLayout) findViewById(R.id.ll_scanner);
		lv_scanner_list = (ListView) findViewById(R.id.lv_scanner_listview);
		btn_scanner_port = (Button) findViewById(R.id.btn_scanner_port);
		btn_scanner_baud = (Button) findViewById(R.id.btn_scanner_baud);
		btn_scanner_parity = (Button) findViewById(R.id.btn_scanner_parity);
		btn_scanner_data_bits = (Button) findViewById(R.id.btn_scanner_data_bits);
		btn_scanner_stop_bits = (Button) findViewById(R.id.btn_scanner_stop_bits);
		btn_scanner_disable_cmd = (Button) findViewById(R.id.btn_scanner_disable_cmd);
		btn_scanner_enable_cmd = (Button) findViewById(R.id.btn_scanner_enable_cmd);
//		btn_scanner_weight_format = (Button) findViewById(R.id.btn_scanner_weight_format);
		et_scanner_data = (EditText) findViewById(R.id.et_scanner_data);
		et_scanner_device_name = (EditText) findViewById(R.id.et_scanner_device_name);
		cb_scanner_active = (CheckBox) findViewById(R.id.cb_scanner_active);

		ll_pinpad = (LinearLayout) findViewById(R.id.ll_pinpad);
		lv_pinpad_list = (ListView) findViewById(R.id.lv_pinpad_listview);
		btn_pinpad_port = (Button) findViewById(R.id.btn_pinpad_port);
		et_pinpad_destip = (EditText) findViewById(R.id.et_pinpad_destip);
		et_pinpad_device_name = (EditText) findViewById(R.id.et_pinpad_device_name);
		et_pinpad_timeout = (EditText) findViewById(R.id.et_pinpad_timeout);
		cb_pinpad_active = (CheckBox) findViewById(R.id.cb_pinpad_active);
		cb_signature = (CheckBox) findViewById(R.id.cb_signature);
		btn_conn_type = (Button) findViewById(R.id.btn_conn_type);
		et_pinpad_destport = (EditText) findViewById(R.id.et_pinpad_destport);

		lv_choose = (ListView) findViewById(R.id.lv_choose);
		btn_save = (Button) findViewById(R.id.btn_save);
		btn_exit = (Button) findViewById(R.id.btn_exit);
		btn_add = (Button) findViewById(R.id.btn_add);
		btn_delete = (Button) findViewById(R.id.btn_delete);
		btn_undo = (Button) findViewById(R.id.btn_undo);
		al_scale = (AbsoluteLayout) findViewById(R.id.al_scale);
		al_scanner = (AbsoluteLayout) findViewById(R.id.al_scanner);
		al_pinpad = (AbsoluteLayout) findViewById(R.id.al_pinpad);
	}

	public void GetScaleDetail(int position) {
		if (ScaleSource.size() <= 0) {
			setEmpty();
			return;
		}
		Scale scale = ScaleSource.get(position);
		btn_scale_port.setText(String.valueOf(scale.getPort()));
		if (scale.getDeviceName() == null || scale.getDeviceName().equals("")) {
			et_scale_device_name.setText("");
		} else {
			et_scale_device_name.setText(String.valueOf(scale.getDeviceName()));
		}
		if (scale.getBaud() == null || scale.getBaud().equals("")) {
			btn_scale_baud.setText("");
		} else {
			btn_scale_baud.setText(String.valueOf(scale.getBaud()));
		}
		SelectionSource = selectionService.getSelections("PARITY");
		if (scale.getParity() == null || scale.getParity().equals("")) {
			btn_scale_parity.setText("");
		} else {
			for (int i = 0; i < SelectionSource.size(); i++) {
				String temp = SelectionSource.get(i).getValue();
				if (scale.getParity().equals(temp.substring(0, 1))) {
					btn_scale_parity.setText(temp);
					break;
				}
			}
		}
		if (String.valueOf(scale.getDataBits()).equals("0")) {
			btn_scale_data_bits.setText("");
		} else {
			btn_scale_data_bits.setText(String.valueOf(scale.getDataBits()));
		}
		if (String.valueOf(scale.getStopBits()).equals("0")) {
			btn_scale_stop_bits.setText("");
		} else {
			btn_scale_stop_bits.setText(String.valueOf(scale.getStopBits()));
		}
		if (scale.getWeightCmd() == null) {
			btn_scale_weight_cmd.setText("");
		} else {
			btn_scale_weight_cmd.setText(String.valueOf(scale.getWeightCmd()));
		}
		if (scale.getCancelCmd() == null) {
			btn_scale_cancel_cmd.setText("");
		} else {
			btn_scale_cancel_cmd.setText(String.valueOf(scale.getCancelCmd()));
		}
		if (String.valueOf(scale.getDivisor()).equals("0")) {
			btn_scale_divide_by.setText("");
		} else {
			btn_scale_divide_by.setText(String.valueOf(scale.getDivisor()));
		}
		et_scale_time_out.setText(String.valueOf(scale.getTimeout()));
		if (String.valueOf(scale.getTimeout()).equals("0")) {
			et_scale_time_out.setText("");
		} else {
			et_scale_time_out.setText(String.valueOf(scale.getTimeout()));
		}
		if (scale.getDataFormat() == null || scale.getDataFormat().equals("")) {
			et_scale_data.setText("");
		} else {
			et_scale_data.setText(String.valueOf(scale.getDataFormat()));
		}
		if (scale.getUsing().equals("Y")) {
			cb_scale_active.setChecked(true);
		} else {
			cb_scale_active.setChecked(false);
		}
	}

	public void GetScannerDetail(int position) {
		if (ScannerSource.size() <= 0) {
			setEmpty();
			return;
		}
		Scanner scanner = ScannerSource.get(position);
		btn_scanner_port.setText(String.valueOf(scanner.getPort()));
		if (scanner.getBaud() == null || scanner.getBaud().equals("")) {
			btn_scanner_baud.setText("");
		} else {
			btn_scanner_baud.setText(String.valueOf(scanner.getBaud()));
		}
		SelectionSource = selectionService.getSelections("PARITY");
		if (scanner.getParity() == null || scanner.getParity().equals("")) {
			btn_scanner_parity.setText("");
		} else {
			for (int i = 0; i < SelectionSource.size(); i++) {
				String temp = SelectionSource.get(i).getValue();
				if (scanner.getParity().equals(temp.substring(0, 1))) {
					btn_scanner_parity.setText(temp);
					break;
				}
			}
		}
		if (scanner.getDeviceName() == null
				|| scanner.getDeviceName().equals("")) {
			et_scanner_device_name.setText("");
		} else {
			et_scanner_device_name.setText(String.valueOf(scanner
					.getDeviceName()));
		}
		if (String.valueOf(scanner.getDataBits()).equals("0")) {
			btn_scanner_data_bits.setText("");
		} else {
			btn_scanner_data_bits
					.setText(String.valueOf(scanner.getDataBits()));
		}
		if (String.valueOf(scanner.getStopBits()).equals("0")) {
			btn_scanner_stop_bits.setText("");
		} else {
			btn_scanner_stop_bits
					.setText(String.valueOf(scanner.getStopBits()));
		}
		if (scanner.getDisableCmd() == null) {
			btn_scanner_disable_cmd.setText("");
		} else {
			btn_scanner_disable_cmd.setText(String.valueOf(scanner
					.getDisableCmd()));
		}
		if (scanner.getEnableCmd() == null) {
			btn_scanner_enable_cmd.setText("");
		} else {
			btn_scanner_enable_cmd.setText(String.valueOf(scanner
					.getEnableCmd()));
		}
//		if (String.valueOf(scanner.getWeightFormat()).equals("0")) {
//			btn_scanner_weight_format.setText("");
//		} else {
//			btn_scanner_weight_format.setText(String.valueOf(scanner
//					.getWeightFormat()));
//		}
		if (scanner.getDataFormat() == null
				|| scanner.getDataFormat().equals("")) {
			et_scanner_data.setText("");
		} else {
			et_scanner_data.setText(String.valueOf(scanner.getDataFormat()));
		}
		if (scanner.getUsing().equals("Y")) {
			cb_scanner_active.setChecked(true);
		} else {
			cb_scanner_active.setChecked(false);
		}
	}

	public void GetPinpadDetail(int position) {
		if (PinpadSource.size() <= 0) {
			setEmpty();
			return;
		}
		Pinpad pinpad = PinpadSource.get(position);
		btn_pinpad_port.setText(String.valueOf(pinpad.getPort()));

		if (pinpad.getDeviceName() == null || pinpad.getDeviceName().equals("")) {
			et_pinpad_device_name.setText("");
		} else {
			et_pinpad_device_name.setText(String.valueOf(pinpad.getDeviceName()));
		}
		et_pinpad_timeout.setText(String.valueOf(pinpad.getTimeout()));
		if (pinpad.getDestip() == null || pinpad.getDestip().equals("")) {
			et_pinpad_destip.setText("");
		} else {
			et_pinpad_destip.setText(String.valueOf(pinpad.getDestip()));
		}
		if (pinpad.getUsing().equals("Y")) {
			cb_pinpad_active.setChecked(true);
		} else {
			cb_pinpad_active.setChecked(false);
		}
		if(pinpad.getSignature_fl().equals("0")){
			cb_signature.setChecked(false);
		}else{
			cb_signature.setChecked(true);
		}
		if (pinpad.getCon_type() == null || pinpad.getCon_type().equals("")) {
			btn_conn_type.setText("");
		}else {
			btn_conn_type.setText(String.valueOf(pinpad.getCon_type()));
		}
		if (pinpad.getDestport() == 0) {
			et_pinpad_destport.setText("");
		}else {
			et_pinpad_destport.setText(String.valueOf(pinpad.getDestport()));
		}
	}

	Dialog DialogWindow = null;

	private void DialogWindows(String Group, String value) {
		if (DialogWindow == null) {
			DialogWindow = new Dialog(this, R.style.dialog2);
			DialogWindow.setContentView(R.layout.hw_dialog_perview_models);
		}

		lv_dialog_list = (ListView) DialogWindow
				.findViewById(R.id.lv_dialog_list);
		btn_dialog_ok = (Button) DialogWindow.findViewById(R.id.btn_dialog_ok);
		btn_dialog_cancel = (Button) DialogWindow
				.findViewById(R.id.btn_dialog_cancel);
		iv_dialog_add = (BeepImageButton) DialogWindow
				.findViewById(R.id.iv_dialog_add);
		iv_dialog_complete = (BeepImageButton) DialogWindow
				.findViewById(R.id.iv_dialog_complete);
		iv_dialog_delete = (BeepImageButton) DialogWindow
				.findViewById(R.id.iv_dialog_delete);
		et_dialog_add = (EditText) DialogWindow
				.findViewById(R.id.et_dialog_add);
		lv_dialog_list.setOnItemClickListener(onItemClickDialogWindowListener);
		btn_dialog_ok.setOnClickListener(OnClickDialogWindowListener);
		btn_dialog_cancel.setOnClickListener(OnClickDialogWindowListener);
		iv_dialog_add.setOnClickListener(OnClickDialogWindowListener);
		iv_dialog_complete.setOnClickListener(OnClickDialogWindowListener);
		iv_dialog_delete.setOnClickListener(OnClickDialogWindowListener);

		lv_dialog_list.setTag(String.valueOf(Group));
		DialogWindow.show();
		SelectionSource = selectionService.getSelections(Group);
			
		selectionAdapter = new SelectionAdapter(this,
					AdapterDataSourceBuilder.readValueFromSettings(SelectionSource));
		lv_dialog_list.setAdapter(selectionAdapter);
		
		if (SelectionSource.size() > 0) {
			int Editable = SelectionSource.get(0).getEditable();
			if (Editable != 1) {
				iv_dialog_add.setVisibility(View.INVISIBLE);
				iv_dialog_delete.setVisibility(View.INVISIBLE);
			} else {
				iv_dialog_add.setVisibility(View.VISIBLE);
				iv_dialog_delete.setVisibility(View.VISIBLE);
			}
		}
		Locate(value);

	}

	public void Locate(String value) {
		for (int i = 0; SelectionSource.size() > i; i++) {
			if (SelectionSource.get(i).getValue().equals(value)) {
				selectionAdapter.ScrollToRow(Integer.valueOf(SelectionSource
						.get(i).getPosition()));
				return;
			}
		}
		selectionAdapter.ScrollToRow(0);
	}

	public Scale GetScaleItem() {
		String port = String.valueOf(btn_scale_port.getText());
		if (port.trim() == null || port.trim().equals("")) {
			ShowMessage("Please select the Com Port.",
					mMessageDialog.MESSAGE_BOX_OK, mMessageDialog.ICON_INFO);
			return null;
		}

		String device = String.valueOf(et_scale_device_name.getText());
		String baud = String.valueOf(btn_scale_baud.getText());
		String parity = String.valueOf(btn_scale_parity.getText());
		if (parity.length() > 0) {
			parity = parity.trim().substring(0, 1);
		} else {
			parity = "";
		}

		String dataBits = String.valueOf(btn_scale_data_bits.getText());
		int int_dataBits = -1;
		if (!dataBits.trim().equals("")) {
			int_dataBits = Integer.valueOf(dataBits.trim());
		}

		String stopBits = String.valueOf(btn_scale_stop_bits.getText());
		int int_stopBits = -1;
		if (!stopBits.trim().equals("")) {
			int_stopBits = Integer.valueOf(stopBits.trim());
		}

		String weightCmd = String.valueOf(btn_scale_weight_cmd.getText());
		String cancelCmd = String.valueOf(btn_scale_cancel_cmd.getText());
		String divideBy = String.valueOf(btn_scale_divide_by.getText());
		String timeOut = String.valueOf(et_scale_time_out.getText());
		String data = String.valueOf(et_scale_data.getText());
		String using = "";
		if (cb_scale_active.isChecked()) {
			using = "Y";
		} else {
			using = "N";
		}

		int no = -1;
		if (currentProcessMode == Update_action) {
			no = Integer.valueOf(ScaleSource.get(
					scaleListAdapter.GetSelectedIndex()).getNo());
		} else {
			no = scaleService.getMaxNo() + 1;
		}

		String endChar = "";
		if (currentProcessMode == Update_action) {
			endChar = String.valueOf(ScaleSource.get(
					scaleListAdapter.GetSelectedIndex()).getEndChar());
		}

		int int_tiemout = -1;
		if (!timeOut.trim().equals("")) {
			int_tiemout = Integer.valueOf(timeOut.trim());
		}

		int int_divideBy = -1;
		if(!MoleQUtil.isNumeric(divideBy)){
			ShowMessage("Please enter a valid Devide By.",
					mMessageDialog.MESSAGE_BOX_OK, mMessageDialog.ICON_INFO);
			return null;
		}
		if (!divideBy.trim().equals("")) {
			int_divideBy = Integer.valueOf(divideBy.trim());
		}

		Scale scale = new Scale(no, Integer.valueOf(port.trim()),
				device.trim(), baud.trim(), parity, int_dataBits, int_stopBits,
				weightCmd.trim(), cancelCmd.trim(), endChar, int_tiemout,
				data.trim(), int_divideBy, using);
		return scale;
	}

	public Scanner GetScannerItem() {
		String port = String.valueOf(btn_scanner_port.getText());
		if (port.trim() == null || port.trim().equals("")) {
			ShowMessage("Please select the Com Port.",
					mMessageDialog.MESSAGE_BOX_OK, mMessageDialog.ICON_INFO);
			return null;
		}
		String baud = String.valueOf(btn_scanner_baud.getText());
		String device = String.valueOf(et_scanner_device_name.getText());
		String parity = String.valueOf(btn_scanner_parity.getText());
		if (parity.length() > 0) {
			parity = parity.trim().substring(0, 1);
		} else {
			parity = "";
		}

		String dataBits = String.valueOf(btn_scanner_data_bits.getText());
		int int_dataBits = -1;
		if (!dataBits.trim().equals("")) {
			int_dataBits = Integer.valueOf(dataBits.trim());
		}

		String stopBits = String.valueOf(btn_scanner_stop_bits.getText());
		int int_stopBits = -1;
		if (!stopBits.trim().equals("")) {
			int_stopBits = Integer.valueOf(stopBits.trim());
		}

		String data = String.valueOf(et_scanner_data.getText());
		String disable_cmd = String.valueOf(btn_scanner_disable_cmd.getText());
		String enable_cmd = String.valueOf(btn_scanner_enable_cmd.getText());
//		String weight = String.valueOf(btn_scanner_weight_format.getText());
//		int int_weight = -1;
//		if(!MoleQUtil.isNumeric(weight)){
//			ShowMessage("Please enter a valid Weight Format.",
//					mMessageDialog.MESSAGE_BOX_OK, mMessageDialog.ICON_INFO);
//			return null;
//		}
//		if (!weight.trim().equals("")) {
//			int_weight = Integer.valueOf(weight.trim());
//		}

		String using = "";
		if (cb_scanner_active.isChecked()) {
			using = "Y";
		} else {
			using = "N";
		}

		int no = -1;
		if (currentProcessMode == Update_action) {
			no = Integer.valueOf(ScannerSource.get(
					scannerListAdapter.GetSelectedIndex()).getNo());
		} else {
			no = scannerService.getMaxNo() + 1;
		}

		String endChar = "";
		if (currentProcessMode == Update_action) {
			endChar = String.valueOf(ScannerSource.get(
					scannerListAdapter.GetSelectedIndex()).getEndChar());
		}

		String chkDigit = "";
		if (currentProcessMode == Update_action) {
			chkDigit = String.valueOf(ScannerSource.get(
					scannerListAdapter.GetSelectedIndex()).getChkDigit());
		}

		Scanner scanner = new Scanner(no, Integer.valueOf(port.trim()),
				device.trim(), baud.trim(), parity, int_dataBits, int_stopBits,
				disable_cmd.trim(), enable_cmd.trim(), endChar, chkDigit,
				data.trim(), -1, using);
		return scanner;
	}

	public Pinpad GetPinpadItem() {
		String port = String.valueOf(btn_pinpad_port.getText());
		if (port.trim() == null || port.trim().equals("")) {
			ShowMessage("Please select the Com Port.",
					mMessageDialog.MESSAGE_BOX_OK, mMessageDialog.ICON_INFO);
			return null;
		}

		String device = String.valueOf(et_pinpad_device_name.getText());
		String timeOut = String.valueOf(et_pinpad_timeout.getText());
		String destip = String.valueOf(et_pinpad_destip.getText());
		Pattern pattern = Pattern
				.compile("\\b((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\b");
		Matcher matcher = pattern.matcher(destip);
		if (matcher.matches() == false) {
			ShowMessage("Invalid IP Format.", mMessageDialog.MESSAGE_BOX_OK,
					mMessageDialog.ICON_INFO);
			return null;
		}

		String using = "";
		if (cb_pinpad_active.isChecked()) {
			using = "Y";
		} else {
			using = "N";
		}

		int no = -1;
		if (currentProcessMode == Update_action) {
			no = Integer.valueOf(PinpadSource.get(
					pinpadAdapter.GetSelectedIndex()).getNo());
		} else {
			no = pinpadService.getMaxNo() + 1;
		}

		int int_tiemout = -1;
		if (!timeOut.trim().equals("")) {
			int_tiemout = Integer.valueOf(timeOut.trim());
		}
		String signature_fl = "0";
		if(cb_signature.isChecked()){
			signature_fl = "1";
		}
		
		String connType = String.valueOf(btn_conn_type.getText());
		if (connType.trim() == null || connType.trim().equals("")) {
			ShowMessage("Please select the Conn Type.",
					mMessageDialog.MESSAGE_BOX_OK, mMessageDialog.ICON_INFO);
			return null;
		}
		String descPort = String.valueOf(et_pinpad_destport.getText());
		if (connType.equals("TCP") && (descPort.trim() == null || descPort.trim().equals(""))) {
			ShowMessage("Please input the Dest Port.",
					mMessageDialog.MESSAGE_BOX_OK, mMessageDialog.ICON_INFO);
			return null;
		}
		int dest_port = -1;
		if (!descPort.trim().equals("")) {
			dest_port = Integer.valueOf(descPort);
		}

		Pinpad pinpad = new Pinpad(no, Integer.valueOf(port.trim()),
				device.trim(), int_tiemout, destip, using, signature_fl, dest_port, connType);
		return pinpad;
	}

	public void Keyboard() {
		InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		inputMethodManager.hideSoftInputFromWindow(HardwareSettingActivity.this
				.getCurrentFocus().getWindowToken(),
				InputMethodManager.HIDE_NOT_ALWAYS);
	}

	public void SetEnabledFalse() {
		lv_choose.setEnabled(false);
		lv_scale_list.setEnabled(false);
		lv_scanner_list.setEnabled(false);
		lv_pinpad_list.setEnabled(false);
		btn_exit.setEnabled(false);
		btn_add.setEnabled(false);
		btn_delete.setEnabled(false);
		btn_undo.setEnabled(true);
		btn_save.setEnabled(true);
	}

	public void SetEnabledTrue() {
		lv_choose.setEnabled(true);
		lv_scale_list.setEnabled(true);
		lv_scanner_list.setEnabled(true);
		lv_pinpad_list.setEnabled(true);
		btn_exit.setEnabled(true);
		btn_add.setEnabled(true);
		btn_delete.setEnabled(true);
		btn_undo.setEnabled(false);
		btn_save.setEnabled(false);
	}

	public void setEmpty() {
		btn_scale_port.setText("");
		btn_scale_baud.setText("");
		btn_scale_parity.setText("");
		btn_scale_data_bits.setText("");
		btn_scale_stop_bits.setText("");
		btn_scale_weight_cmd.setText("");
		btn_scale_cancel_cmd.setText("");
		btn_scale_divide_by.setText("");
		et_scale_time_out.setText("");
		et_scale_data.setText("");
		et_scale_device_name.setText("");
		cb_scale_active.setChecked(false);

		btn_scanner_port.setText("");
		btn_scanner_baud.setText("");
		btn_scanner_parity.setText("");
		btn_scanner_data_bits.setText("");
		btn_scanner_stop_bits.setText("");
		btn_scanner_disable_cmd.setText("");
		btn_scanner_enable_cmd.setText("");
//		btn_scanner_weight_format.setText("");
		et_scanner_data.setText("");
		et_scanner_device_name.setText("");
		cb_scanner_active.setChecked(false);

		btn_pinpad_port.setText("");
		et_pinpad_device_name.setText("");
		et_pinpad_destip.setText("");
		et_pinpad_timeout.setText("");
		cb_pinpad_active.setChecked(false);
		cb_signature.setChecked(false);
		btn_conn_type.setText("");
		et_pinpad_destport.setText("");
	}

	public void ShowMessage(String text, int type, int icon) {
		if (mMessageDialog != null && mMessageDialog.isShowing()) {
			mMessageDialog.dismiss();
		}
		mMessageDialog = new MessageDialog(HardwareSettingActivity.this);
		mMessageDialog.setStyle(type);
		mMessageDialog.setMessageIcon(icon);
		mMessageDialog.setMessage(text);
		mMessageDialog.setOnClickMessageListener(onClickMessageListener);
		mMessageDialog.show();
	}



	private OnClickMessageListener onClickMessageListener = new OnClickMessageListener() {

		@Override
		public void onClickYes() {
			// TODO Auto-generated method stub
			int position, no;
			if (isDelete) {
				if (isDialogItem) {
					position = selectionAdapter.GetSelectedIndex();
					String Value = SelectionSource.get(position).getValue();
					String Group = SelectionSource.get(position).getGroup();
					selectionService.DeleteSelection(Group, Value);
					SelectionSource.clear();
					SelectionSource = selectionService.getSelections(Group);
					selectionAdapter
							.setSource(AdapterDataSourceBuilder.readValueFromSettings(SelectionSource));
					selectionAdapter.notifyDataSetChanged();
					if (position > 0) {
						position = position - 1;
					}
					selectionAdapter.ScrollToRow(position);
					isDialogItem = false;
				} else {
					switch (CurrentMode) {
					case SCALE_SETTING:
						position = scaleListAdapter.GetSelectedIndex();
						no = ScaleSource.get(position).getNo();
						scaleService.deleteItem(no);
//						if (GlobalInfo.getInstance().isMGNetwork) {
//							Scale scale = scaleService.FindScaleByNo(no);
//							if (scale != null) {
//								mMGCreateFiles.createrFile(scale
//										.toMGData("DELETE"));// UPDATE,DELETE
//							}
//						}
						ScaleSource.clear();
						ScaleSource = scaleService.getAllScales();
						scaleListAdapter.setSource(ScaleSource);
						scaleListAdapter.notifyDataSetChanged();
						if (position > 0) {
							position = position - 1;
						}
						editChangeMode = SYSTEM_EDITED;
						GetScaleDetail(position);
						scaleListAdapter.ScrollToRow(position);
						editChangeMode = USER_EDITED;
						break;
					case SCANNER_SETTING:
						position = scannerListAdapter.GetSelectedIndex();
						no = ScannerSource.get(position).getNo();
						scannerService.deleteItem(no);
//						if (GlobalInfo.getInstance().isMGNetwork) {
//							Scanner scanner = scannerService
//									.FindScannerByNo(no);
//							if (scanner != null) {
//								mMGCreateFiles.createrFile(scanner
//										.toMGData("DELETE"));// UPDATE,DELETE
//							}
//						}
						ScannerSource.clear();
						ScannerSource = scannerService.getAllScanner();
						scannerListAdapter.setSource(ScannerSource);
						scannerListAdapter.notifyDataSetChanged();
						if (position > 0) {
							position = position - 1;
						}
						editChangeMode = SYSTEM_EDITED;
						GetScannerDetail(position);
						scannerListAdapter.ScrollToRow(position);
						editChangeMode = USER_EDITED;
						break;
					case PINPAD_SETTING:
						position = pinpadAdapter.GetSelectedIndex();
						no = PinpadSource.get(position).getNo();
						pinpadService.deletePinpad(no);
//						if (GlobalInfo.getInstance().isMGNetwork) {
//							Pinpad pinpad = pinpadService.FindPinpadByNo(no);
//							if (pinpad != null) {
//								mMGCreateFiles.createrFile(pinpad
//										.toMGData("DELETE"));// UPDATE,DELETE
//							}
//						}
						PinpadSource.clear();
						PinpadSource = pinpadService.getAllPinpad();
						pinpadAdapter.setSource(PinpadSource);
						pinpadAdapter.notifyDataSetChanged();
						if (position > 0) {
							position = position - 1;
						}
						editChangeMode = SYSTEM_EDITED;
						GetPinpadDetail(position);
						pinpadAdapter.ScrollToRow(position);
						editChangeMode = USER_EDITED;
						break;
					}
				}
				isDelete = false;
				mMessageDialog.dismiss();
			} else {
				finish();
			}
		}

		@Override
		public void onClickNo() {
			// TODO Auto-generated method stub
			isDelete = false;
			mMessageDialog.dismiss();
		}

		@Override
		public void onClickOK() {
			// TODO Auto-generated method stub
			mMessageDialog.dismiss();
		}

	};
}